/* ==================================================================   
 * Created [2009-4-27 下午11:32:55] by Jon.King 
 * ==================================================================  
 * TSS 
 * ================================================================== 
 * mailTo:jinpujun@hotmail.com
 * Copyright (c) Jon.King, 2009-2012 
 * ================================================================== 
 */

package com.jinhe.tss.component.support.web.upload;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import com.jinhe.tss.component.support.web.webwork.BaseActionSupport;
import com.jinhe.tss.core.exception.BusinessException;
import com.jinhe.tss.core.util.EasyUtils;

/**
 * 文件上传Action
 */
public class FileUploadAction extends BaseActionSupport {

    private static final long serialVersionUID = 3834494967429903133L;

    /**
     * 客户端上传文件，用户远程小文件上传
     */
    private File file;

    /**
     * 服务器端本地文件全路径，用于本地大文件上传
     */
    private String localPath;

    /**
     * 用于文件上传、下载过程中保存其他参数
     */
    private Map<String, String> params = new HashMap<String, String>();

    private FileUploadManager manager;

    /**
     * 执行文件上传
     */
    public String execute() {
        if (file == null && !EasyUtils.isNullOrEmpty(localPath) )
            file = new File(localPath); //如果没有远程文件上传，则使用本地文件
        
        if (file == null) 
            throw new BusinessException("没有指定上传的文件或文件太大或文件名称太长，请重新上传");
        
        print(manager.upload(file, params));
        return XML;
    }

    public void setFile(File file) {
        this.file = file;
    }

    public void setLocalPath(String localPath) {
        this.localPath = localPath;
    }

    public Map<String, String> getParams() {
        return params;
    }

    /**
     * 设置文件传输对象
     */
    public void setManager(FileUploadManager manager) {
        this.manager = manager;
    }
}
